/**
 * Util.java
 * 版权所有(C) 2014
 * 创建者:cuiran 2014-7-22 上午9:38:33
 */
package com.ghyf.mplay.util;

import org.json.JSONArray;
import org.json.JSONException;
import org.videolan.libvlc.LibVLC;
import org.videolan.libvlc.LibVlcException;

import com.ghyf.mplay.application.BaseCookie;
import com.ghyf.mplay.application.CustomCrashHandler;


import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.preference.PreferenceManager;

/**
 * TODO 
 * @author cuiran
 * @version 1.0.0
 */
public class Util {

	  public static LibVLC getLibVlcInstance() throws LibVlcException {
	        LibVLC instance = LibVLC.getExistingInstance();
	        if (instance == null) {
	            Thread.setDefaultUncaughtExceptionHandler(new CustomCrashHandler());

	            instance = LibVLC.getInstance();
	            Context context = BaseCookie.getAppContext();
	            SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(context);
	            updateLibVlcSettings(pref);
	            instance.init(context);
	        }
	        return instance;
	    }
	  
	  public static float[] getFloatArray(SharedPreferences pref, String key) {
	        float[] array = null;
	        String s = pref.getString(key, null);
	        if (s != null) {
	            try {
	                JSONArray json = new JSONArray(s);
	                array = new float[json.length()];
	                for (int i = 0; i < array.length; i++)
	                    array[i] = (float) json.getDouble(i);
	            } catch (JSONException e) {
	                e.printStackTrace();
	            }
	        }
	        return array;
	    }

	    public static void putFloatArray(Editor editor, String key, float[] array) {
	        try {
	            JSONArray json = new JSONArray();
	            for (float f : array)
	                json.put(f);
	            editor.putString("equalizer_values", json.toString());
	        } catch (JSONException e) {
	            e.printStackTrace();
	        }
	    }


	  public static void updateLibVlcSettings(SharedPreferences pref) {
	        LibVLC instance = LibVLC.getExistingInstance();
	        if (instance == null)
	            return;

	        instance.setSubtitlesEncoding(pref.getString("subtitle_text_encoding", ""));
	        instance.setTimeStretching(pref.getBoolean("enable_time_stretching_audio", false));
	        instance.setFrameSkip(pref.getBoolean("enable_frame_skip", false));
	        instance.setChroma(pref.getString("chroma_format", ""));
	        instance.setVerboseMode(pref.getBoolean("enable_verbose_mode", true));

	        if (pref.getBoolean("equalizer_enabled", false))
	            instance.setEqualizer(getFloatArray(pref, "equalizer_values"));

	        int aout;
	        try {
	            aout = Integer.parseInt(pref.getString("aout", "-1"));
	        }
	        catch (NumberFormatException nfe) {
	            aout = -1;
	        }
	        int vout;
	        try {
	        	vout = Integer.parseInt(pref.getString("vout", "-1"));
	        }
	        catch (NumberFormatException nfe) {
	        	vout = -1;
	        }
	        int deblocking;
	        try {
	            deblocking = Integer.parseInt(pref.getString("deblocking", "-1"));
	        }
	        catch(NumberFormatException nfe) {
	            deblocking = -1;
	        }
	        int hardwareAcceleration;
	        try {
	            hardwareAcceleration = Integer.parseInt(pref.getString("hardware_acceleration", "-1"));
	        }
	        catch(NumberFormatException nfe) {
	            hardwareAcceleration = -1;
	        }
	        int networkCaching = pref.getInt("network_caching_value", 0);
	        if(networkCaching > 60000)
	            networkCaching = 60000;
	        else if(networkCaching < 0)
	            networkCaching = 0;
	        instance.setAout(aout);
	        instance.setVout(vout);
	        instance.setDeblocking(deblocking);
	        instance.setNetworkCaching(networkCaching);
	        instance.setHardwareAcceleration(hardwareAcceleration);
	    }
}
